package pro.cherish.smartfile.config.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.FluxSink;

/**
 * @author lys
 */
@Slf4j
public class WebSocketSender {
    private WebSocketSession session;
    private FluxSink<WebSocketMessage> sink;

    public WebSocketSender(WebSocketSession session, FluxSink<WebSocketMessage> sink) {
        this.session = session;
        this.sink = sink;
    }

    public void sendData(String data) {
        try {
            sink.next(session.textMessage(data));
        } catch (Exception e) {
            log.error("websocket发送失败,错误信息={}",e.toString());
        }
    }
}